﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace 行政区域
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            try
            {
                ShowXmlTreeNode();
            }
            catch (Exception)
            {

            }
        }
        public void LocalData(List<Regional> list, int pid, TreeNodeCollection nodes)
        {
            foreach (var item in list)
            {
                if (item.PId == pid)
                {
                    if (item.Name.Length == 0)
                        item.Name = "县";

                    TreeNode node = new TreeNode();
                    node.Text = item.Name;
                    node.Tag = "http://www.xzqy.net/" + item.Code + ".htm";
                    node.ToolTipText = item.Code;

                    //XzqyNet xzqy = new XzqyNet(node.Tag.ToString());
                    //var _list = xzqy.TestSetRelationRegional();
                    //foreach (var item1 in _list)
                    //{
                    //    TreeNode node1 = new TreeNode();
                    //    node1.Text = item1.Name + "-" + item1.Code;
                    //    node1.Tag = "http://www.xzqy.net/" + item1.Code + ".htm";
                    //    node.ToolTipText = item1.Code;
                    //    node.Nodes.Add(node1);
                    //}

                    nodes.Add(node);
                    if (item.SonRegionals.Count > 0)
                        LocalData(item.SonRegionals, item.Id, node.Nodes);

                }
            }
        }
        private void 读取ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var node = this.treeView1.SelectedNode;
            if (node != null)
            {
                if (node.Nodes.Count == 0)
                {
                    string url = node.Tag.ToString();
                    XzqyNet xzqy = new XzqyNet(url);
                    var list = xzqy.TestSetRelationRegional();
                    LocalData(list, 0, this.treeView1.SelectedNode.Nodes);
                }
            }
            MessageBox.Show("读取成功");
        }
        private void 入库ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("Xzqy.XML");
            XmlNodeList xmlNodes = xmlDoc.DocumentElement.ChildNodes;
            XmlNode2Db(xmlNodes);
            MessageBox.Show("入库成功");
        }
        private void XmlNode2Db(XmlNodeList xmlNode)
        {
            foreach (XmlNode var in xmlNode)
            {
                if (var.NodeType != XmlNodeType.Element)
                {
                    continue;
                }
                Xzqy xzqy = new Xzqy();
                xzqy.HelperCode = var.Attributes["HelperCode"].Value;
                xzqy.Name = var.Attributes["Name"].Value;
                xzqy.IsLast = var.HasChildNodes;
                xzqy.Level = Convert.ToInt32(var.Attributes["Level"].Value) + 1;
                xzqy.NodePath = var.Attributes["NodePath"].Value;
                xzqy.OrderBy = Convert.ToInt32(var.Attributes["OrderBy"].Value);
                string pid = var.Attributes["ParentId"].Value;
                if (pid != Guid.Empty.ToString())
                {
                    var dt = DbHelperSQL.Query("SELECT * FROM Xzqy WHERE HelperCode='" + pid + "'").Tables[0];
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        xzqy.ParentId = Guid.Parse(dt.Rows[0]["ID"].ToString());
                    }
                }
                xzqy.Add();
                if (var.HasChildNodes)
                {
                    XmlNode2Db(var.ChildNodes);
                }
            }
        }
        XmlDocument doc = new XmlDocument();
        private void 保存XMLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            doc.LoadXml("<Menu></Menu>");
            XmlNode root = doc.DocumentElement;
            doc.InsertBefore(doc.CreateXmlDeclaration("1.0", "utf-8", "yes"), root);
            TreeNode2Xml(this.treeView1.Nodes, root);
            doc.Save("Xzqy.XML");
            MessageBox.Show("XML保存成功");
        }
        private void TreeNode2Xml(TreeNodeCollection treeNodes, XmlNode xmlNode)
        {
            XmlDocument doc = xmlNode.OwnerDocument;
            foreach (TreeNode treeNode in treeNodes)
            {
                XmlNode element = doc.CreateNode("element", "Item", "");
                XmlAttribute attr = doc.CreateAttribute("HelperCode");
                attr.Value = treeNode.ToolTipText.ToString();
                element.Attributes.Append(attr);

                XmlAttribute attr1 = doc.CreateAttribute("Name");
                attr1.Value = treeNode.Text;
                element.Attributes.Append(attr1);

                XmlAttribute attr2 = doc.CreateAttribute("Level");
                attr2.Value = treeNode.Level.ToString();
                element.Attributes.Append(attr2);

                XmlAttribute attr3 = doc.CreateAttribute("NodePath");
                attr3.Value = treeNode.FullPath;
                element.Attributes.Append(attr3);

                XmlAttribute attr4 = doc.CreateAttribute("OrderBy");
                attr4.Value = treeNode.Index.ToString();
                element.Attributes.Append(attr4);

                if (treeNode.Parent != null)
                {
                    XmlAttribute attr5 = doc.CreateAttribute("ParentId");
                    attr5.Value = treeNode.Parent.ToolTipText.ToString();
                    element.Attributes.Append(attr5);
                }
                else
                {
                    XmlAttribute attr5 = doc.CreateAttribute("ParentId");
                    attr5.Value = "";
                    element.Attributes.Append(attr5);
                }

                XmlAttribute attr6 = doc.CreateAttribute("url");
                attr6.Value = treeNode.Tag.ToString();
                element.Attributes.Append(attr6);

                xmlNode.AppendChild(element);

                if (treeNode.Nodes.Count > 0)
                {
                    TreeNode2Xml(treeNode.Nodes, element);
                }
            }
        }
        private void ShowXmlTreeNode()
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("Xzqy.XML");

            XmlNodeList xmlNodes = xmlDoc.DocumentElement.ChildNodes;

            this.treeView1.BeginUpdate();
            this.treeView1.Nodes.Clear();
            XmlNode2TreeNode(xmlNodes, this.treeView1.Nodes);
            this.treeView1.EndUpdate();


        }
        private void XmlNode2TreeNode(XmlNodeList xmlNode, TreeNodeCollection treeNode)
        {
            foreach (XmlNode var in xmlNode)
            {
                if (var.NodeType != XmlNodeType.Element)
                {
                    continue;
                }
                TreeNode newTreeNode = new TreeNode();
                newTreeNode.Text = var.Attributes["Name"].Value;
                newTreeNode.Tag = var.Attributes["url"].Value;
                newTreeNode.ToolTipText = var.Attributes["HelperCode"].Value;
                if (var.HasChildNodes)
                {
                    if (var.ChildNodes[0].NodeType == XmlNodeType.CDATA)
                    {
                        newTreeNode.Tag = var.ChildNodes[0].Value;
                    }
                    XmlNode2TreeNode(var.ChildNodes, newTreeNode.Nodes);
                }
                treeNode.Add(newTreeNode);
            }
        }
        private void 读取最后一级数据ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2();
            frm.ShowDialog();
        }
    }
}
